<?php
namespace api\home\controller;

use think\Db;
use think\Validate;
use cmf\controller\RestBaseController;
use app\common\model\TemplateCategoryModel;
use app\common\model\TemplateGroupModel;
use app\common\model\MytaskModel;
use app\common\model\TemplateAttrModel;

class TemplateGroupController extends RestBaseController
{
	/**
	 * [模板名称]
	 * @param [type] $[param.category_id] [活动模板ID]
	 * @return [type] [description]
	 */
	public function name(){
		$param = $this->request->param();
		$data = TemplateCategoryModel::where('category_id',$param['category_id'])->value('name');
		$this->success('',$data);
	}
	/**
	 * [获取最新的一条套件]
	 * @param [type] $[param.category_id] [活动模板ID]
	 * @return [type] [description]
	 */
	public function one(){
		$param = $this->request->param();
		$data =TemplateGroupModel::relation('template')
					->where('category_id',$param['category_id'])
					->order('group_id desc')
					->find();
		if(!empty($data)){
			$this->success('',$data->toArray());
		}else{
			$this->error('');
		}
	}
	/**
	 * [获取某一ID套件详情]
	 * @param [type] $[param.group_id] [套件ID]
	 * @return [type] [description]
	 */
	public function getOne(){
		$param = $this->request->param();
		$data = TemplateGroupModel::get($param['group_id'],'template,template.classprogress,category,header,footer')->toArray();
		if(!empty($data['header'])){
			$data['header']['content'] = json_decode($data['header']['content']);
		}
		if(!empty($data['footer'])){
			$data['footer']['content'] = json_decode($data['footer']['content']);
		}
		for ($i=1; $i <= $data['template']['progress'] ; $i++) { 
			$name = TemplateAttrModel::where('template_id',$data['template']['id'])->where('progress',$i)->value('name');
			$data['name'][$i] = empty($name) ? '' : $name;
		}

		$this->success('',$data);
	}
    /**
     * [活动模板下的套件列表]
     * @param [type] $[param.category_id] [活动模板ID]
     * @return [type] [description]
     */
	public function index(){
		$param = $this->request->param();
		$page = empty($param['page']) ? 1 : $param['page'];

		$data =TemplateGroupModel::relation('template.classprogress')
					->where('category_id',$param['category_id'])
					->where('status',1)
					->order('group_id desc')
					->paginate(20,false,['page'=>$page]);

		if(!empty($data->items())){
			$this->success('',$data->items());
		}else{
			$this->error('');
		}
	}
	/**
	 * [我的课堂套件]
	 * @param [type] $[param.category_id] [活动模板ID]
	 * @return [type] [description]
	 */
	public function myList(){
		$user_id = $this->getUserId();
		$param = $this->request->param();
		$data = MytaskModel::where('uid',$user_id)
					->where('template_category_id',$param['category_id'])
					->field('id,name,description,create_time')
					->order('id desc')
					->limit('0,30')
					->select()
					->each(function($value,$item) use($user_id){
							$Id = $value['id'];
						  	$data=Db::name('mytask')->alias('m')->join('t_template t','m.t_id = t.id')->where('m.id='.$Id)->field('progress')->find();//模板信息
					        $reply=Db::name('anser')->where(['userId'=>$user_id,'taskid'=>$Id])->select()->toArray();
					        $reply2=Db::name('anser')->where(['userId'=>$user_id,'taskid'=>$Id,'status'=>0])->select()->toArray();
					        $cnt = count($reply);
					        $cnt2 = count($reply2);
					        if($cnt==0){
					            //未开始
					            $value['status'] = 0;
					            $value['status_name'] = '未开始';
					        }elseif ($cnt2 == $data['progress']) {
					            //已完成
					            $value['status'] = 2;
					            $value['status_name'] = '已完成';
					        }else{
					            //进行中
					            $value['status'] = 1;
					            $value['status_name'] = '进行中';
					        }
					});

		if(!empty($data->toArray())){
			$this->success('',$data->toArray());
		}else{
			$this->error('');
		}
	}

}
